/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       dialog_charm.cpp

	$Header: /game/dialog_charm.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "dialog_charm.h"

#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_window.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "creature.h"
#include "creature_array.h"
#include "creature_select_window.h"
#include "creature_traits.h"
#include "external_string.h"
#include "format_string.h"
#include "materials.h"
#include "replace_keywords.h"
#include "skill.h"
#include "skill_icon.h"
#include "standard_fonts.h"
#include "text_window.h"

extern t_external_string const k_hotkey_buy;
static t_bitmap_group_cache const k_bitmaps_charm_or_diplomacy( "dialog.charm_or_diplomacy" );
static t_bitmap_group_cache const k_bitmaps_charm_and_diplomacy( "dialog.charm_and_diplomacy" );

static t_external_string const k_text_charm_title( "charm_title.charmdialog" );
static t_external_string const k_text_diplomacy_title( "diplomacy_title.charmdialog" );
static t_external_string const k_text_diplomacy( "diplomacy.charmdialog" );
static t_external_string const k_text_charm( "charm.charmdialog" );
static t_external_string const k_text_charm_and_diplomacy( "charm_and_diplomacy.charmdialog" );
static t_external_string const k_text_diplomacy_help( "diplomacy_help.misc" );
static t_external_string const k_text_diplomacy_button_help( "diplomacy_button_help.misc" );
static t_external_string const k_text_diplomacy_and_charm_help( "diplomacy_and_charm_help.misc" );
static t_external_string const k_text_charm_but_not_diplomacy( "charm_but_not_diplomacy.misc" );
static t_external_string const k_text_close_help( "close_help.misc" );
extern t_button_cache const		k_buy_button;
extern t_button_cache			g_ok_button;
extern t_button_cache const		k_close_button;

// ---------------------------------------------------------
// dialog to handle pre-combat charms
// ---------------------------------------------------------
t_dialog_charm::t_dialog_charm( t_creature_array const& diplomacy_creatures,
								t_creature_array const& charm_creatures,
								t_skill_mastery			diplomacy,
								t_skill_mastery			charm,
								int						funds,
								t_window*				parent )
			  : t_window( k_completely_transparent, parent )
{
	t_screen_rect rect;
	t_screen_rect parent_rect;
	bool create_title = false;

	if (diplomacy_creatures.empty() || charm_creatures.empty())
	{
		m_bitmaps = k_bitmaps_charm_or_diplomacy.get();
		create_title = true;
	} else
		m_bitmaps = k_bitmaps_charm_and_diplomacy.get();

	rect = m_bitmaps->get_rect();
	parent_rect = parent->get_client_rect();
	rect += (parent_rect.size() - rect.size()) / 2;
	init( rect );

	// create background
	t_bitmap_layer const* layer;
	t_window*             window;
	t_window*			  background;
	t_screen_point		  zero(0,0);
	t_screen_point		  origin;

	layer = m_bitmaps->find( "top" );
	window = new t_bitmap_layer_window( layer, zero, this );
	layer = m_bitmaps->find( "left" );
	window = new t_bitmap_layer_window( layer, zero, this );
	layer = m_bitmaps->find( "right" );
	window = new t_bitmap_layer_window( layer, zero, this );
	layer = m_bitmaps->find( "bottom" );
	window = new t_bitmap_layer_window( layer, zero, this );
	layer = m_bitmaps->find( "background" );
	origin = layer->get_rect().top_left();
	background = new t_bitmap_layer_window( layer, zero, this );

	set_primary_text( background, origin, diplomacy_creatures, charm_creatures );

	// create title text ( for charm OR diplomacy dialog )
	if( create_title )
	{
		t_text_window* text_window;
		layer = m_bitmaps->find( "title" );
		std::string text;
		if (diplomacy_creatures.empty() )
			text = k_text_charm_title;
		else
			text = k_text_diplomacy_title;

		text_window = new t_text_window( get_font( 20 ), layer->get_rect(), this, text, 
										 t_pixel_24(0,0,0) );
		text_window->set_center_horizontal();
		text_window->set_center_vertical();
		text_window->set_drop_shadow( true );
	}

	// set diplomacy creatures
	if (!diplomacy_creatures.empty())
		set_diplomacy( background,origin, diplomacy_creatures, diplomacy, funds,
		               !charm_creatures.empty() );
	if (!charm_creatures.empty())
		set_charm( background, origin, charm_creatures, charm,
				   !diplomacy_creatures.empty() );

	// add cancel button
	t_button*		 button;
	t_button_handler handler;

	rect = m_bitmaps->find( "close_button" )->get_rect() - origin;
	button = new t_button( k_close_button.get(), rect.top_left(), background );
	handler = bound_handler( *this, &t_dialog_charm::close_clicked );
	button->set_click_handler( handler );
	button->set_help_balloon_text( k_text_close_help );
}

// ---------------------------------------------------------
// dialog to handle pre-combat charms
// ---------------------------------------------------------
void t_dialog_charm::set_primary_text( t_window* background, t_screen_point const& origin,
									   t_creature_array const& diplomacy_creatures,
									   t_creature_array const& charm_creatures )
{
	// compute diplomacy cost
	int i;

	m_diplomacy_cost = 0;
	for (i = 0; i < t_creature_array::k_size; ++i)
	{
		if (diplomacy_creatures[i].get_number() == 0)
			continue;
		m_diplomacy_cost += diplomacy_creatures[i].get_creature()->get_traits().cost[k_gold]
						  *	diplomacy_creatures[i].get_number();
	}
	m_diplomacy_cost >>= 1;


	// get text
	std::string    text;
	t_text_window* text_window;
	t_screen_rect	rect;

	if (charm_creatures.empty())
		text = k_text_diplomacy;
	else if (diplomacy_creatures.empty())
		text = k_text_charm;
	else
		text = k_text_charm_and_diplomacy;
	text = replace_keywords( text, m_diplomacy_cost, k_gold );
	rect = m_bitmaps->find( "text" )->get_rect() - origin;
	text_window = new t_text_window( get_font( 20 ), rect, background, text, 
								     t_pixel_24(0,0,0) );
	text_window->set_center_vertical();
	text_window->set_drop_shadow( false );
}

// ---------------------------------------------------------
// dialog to handle pre-combat charms
// ---------------------------------------------------------
void t_dialog_charm::add_creatures( t_window* background, t_screen_point const& origin,
									t_creature_array const& creatures, 
									std::string const& layer_name )
{
	// set creatures
	int							i;
	t_creature_select_window*	creature_window;
	t_screen_rect				rect;
	int							count;

	rect = m_bitmaps->find( layer_name )->get_rect() - origin;
	creature_window = new t_creature_select_window( rect.top_left(), false, background );
	for (i = 0; i < t_creature_array::k_size; ++i)
		if (creatures[i].get_number() > 0)
		{
			count = creature_window->get_count();
			creature_window->add( &creatures[i] );
			creature_window->set_help_text( count, creatures[i].get_name() );
		}
}

// ---------------------------------------------------------
// dialog to handle pre-combat charms
// ---------------------------------------------------------
void t_dialog_charm::set_diplomacy( t_window* background, t_screen_point const& origin,
									t_creature_array const& diplomacy_creatures,
									t_skill_mastery			diplomacy,
									int						funds,
									bool					has_charm )
{
	t_bitmap_layer const*		layer;
	t_screen_rect				rect;
	t_window*					window;
	t_text_window*				text_window;
	t_skill						skill;
	t_button*					button;
	t_button_handler			handler;
	std::string					text;
	std::string                 gold_help;

	// set gold value
	gold_help = k_text_diplomacy_help;
	gold_help = replace_keywords( gold_help,  m_diplomacy_cost, k_gold );
	layer = m_bitmaps->find( "gold_background" );
	window = new t_bitmap_layer_window( layer, -origin, background );
	window->set_help_balloon_text( gold_help );
	layer = m_bitmaps->find( "gold" );
	window = new t_bitmap_layer_window( layer, -origin, background );
	window->set_help_balloon_text( gold_help );
	rect = m_bitmaps->find( "gold_text" )->get_rect() - origin;
	text = format_string( "%i", m_diplomacy_cost );
	text_window = new t_text_window( get_font( rect.height() ), rect, background,
									 text, t_pixel_24(0,0,0));
	text_window->set_center_horizontal();
	text_window->set_drop_shadow( false );

	// set diplomacy icon
	rect = m_bitmaps->find( "diplomacy" )->get_rect() - origin;
	skill.skill = k_skill_diplomacy;
	skill.level = diplomacy;
	window = new t_skill_icon( skill, rect, background );

	add_creatures( background, origin, diplomacy_creatures, "diplomacy_creatures" );

	// create buy button
	rect = m_bitmaps->find( "buy_button" )->get_rect() - origin;
	button = new t_button( k_buy_button.get(), rect.top_left(), background );
	handler = bound_handler( *this, &t_dialog_charm::buy_clicked );
	button->set_click_handler( handler );
	if (has_charm)
		button->set_help_balloon_text( k_text_diplomacy_and_charm_help );
	else
		button->set_help_balloon_text( k_text_diplomacy_button_help );
	if (funds < m_diplomacy_cost)
		button->enable( false );
}

// ---------------------------------------------------------
// dialog to handle pre-combat charms
// ---------------------------------------------------------
static t_external_string const k_text_charm_help( "charm_help.misc" );
void t_dialog_charm::set_charm( t_window* background, t_screen_point const& origin,
								t_creature_array const& charm_creatures,
								t_skill_mastery			charm,
								bool					has_diplomacy )
{
	t_screen_rect				rect;
	t_window*					window;
	t_skill						skill;
	t_button*					button;
	t_button_handler			handler;

	// set diplomacy icon
	rect = m_bitmaps->find( "charm" )->get_rect() - origin;
	skill.skill = k_skill_charm;
	skill.level = charm;
	window = new t_skill_icon( skill, rect, background );

	// set creatures
	add_creatures( background,origin, charm_creatures, "charm_creatures" );

	// create buy button
	rect = m_bitmaps->find( "ok_button" )->get_rect() - origin;
	button = new t_button( g_ok_button.get(), rect.top_left(), background );
	handler = bound_handler( *this, &t_dialog_charm::ok_clicked );
	button->set_click_handler( handler );
	if (has_diplomacy)
		button->set_help_balloon_text( k_text_charm_but_not_diplomacy );
	else
		button->set_help_balloon_text( k_text_charm_help );
}


// ---------------------------------------------------------
// dialog to handle pre-combat charms
// ---------------------------------------------------------
void t_dialog_charm::buy_clicked( t_button* )
{
	m_return_value = k_result_diplomacy;
	close();
}

// ---------------------------------------------------------
// dialog to handle pre-combat charms
// ---------------------------------------------------------
void t_dialog_charm::close_clicked( t_button* )
{
	m_return_value = k_result_cancel;
	close();
}

// ---------------------------------------------------------
// dialog to handle pre-combat charms
// ---------------------------------------------------------
void t_dialog_charm::ok_clicked( t_button* )
{
	m_return_value = k_result_charm;
	close();
}

